package gmu.swe.project.registry;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.Point;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class RegistryUI {
	
	/* Two threads for the Registry Server, one to handle tracker registration
	 * and updates, another one to handle tracker keep alive messages. The keep
	 * alive thread is a single thread that gets started and run on a schedule.
	 * Part of the command line argument is to provide the schedule interval for
	 * the keep alive service thread. The registry service thread listens for
	 * incoming tracker requests and spawns off a new thread for each request.
	 * 
	 * @param args
	 */
	private static RegistryUI rUI = new RegistryUI();  //  @jve:decl-index=0:
	private JPanel jPanel = null;
	private JButton btnConnect = null;
	private JTextField txtRegistry = null;
	private JTextField txtInterval = null;
	private JTextField txtSocket = null;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="10,10"
	private JPanel jContentPane = null;
	private JLabel jLabel3 = null;
	private static JTextField txtConfigFile = null;
	private JTextArea jTextArea = null;
	private JScrollPane jScrollPane = null;
	private JFrame getJFrame() {
		if (jFrame == null) {
			jFrame = new JFrame();
			jFrame.setSize(new Dimension(562, 415));
			jFrame.setTitle("Registry");
			jFrame.setContentPane(getJContentPane());
			jFrame.addWindowListener(new java.awt.event.WindowAdapter() {
				public void windowClosing(java.awt.event.WindowEvent e) {
					System.exit(0);
				}
			});
		}
		return jFrame;
	}
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.NORTH);
			jContentPane.add(getJScrollPane(), BorderLayout.CENTER);
		}
		return jContentPane;
	}
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel3 = new JLabel();
			jLabel3.setText("Config Loc:");
			jLabel3.setSize(new Dimension(115, 18));
			jLabel3.setLocation(new Point(227, 29));
			jLabel2 = new JLabel();
			jLabel2.setText("Interval:");
			jLabel2.setLocation(new Point(12, 29));
			jLabel2.setSize(new Dimension(85, 18));
			jLabel1 = new JLabel();
			jLabel1.setText("Socket Number:");
			jLabel1.setSize(new Dimension(114, 18));
			jLabel1.setLocation(new Point(227, 6));
			jLabel = new JLabel();
			jLabel.setText("Registry ID:");
			jLabel.setSize(new Dimension(86, 18));
			jLabel.setLocation(new Point(11, 6));
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.setPreferredSize(new Dimension(10, 55));
			jPanel.add(getBtnConnect(), null);
			jPanel.add(getTxtRegistry(), null);
			jPanel.add(getTxtInterval(), null);
			jPanel.add(getTxtSocket(), null);
			jPanel.add(jLabel, null);
			jPanel.add(jLabel1, null);
			jPanel.add(jLabel2, null);
			jPanel.add(jLabel3, null);
			jPanel.add(getTxtConfigFile(), null);
		}
		return jPanel;
	}
	private JButton getBtnConnect() {
		if (btnConnect == null) {
			btnConnect = new JButton();
			btnConnect.setPreferredSize(new Dimension(81, 15));
			btnConnect.setLocation(new Point(449, 6));
			btnConnect.setSize(new Dimension(93, 18));
			btnConnect.setText("Connect");
			btnConnect.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ConnectClicked();
				}
			});
		}
		return btnConnect;
	}
	private JTextField getTxtRegistry() {
		if (txtRegistry == null) {
			txtRegistry = new JTextField();
			txtRegistry.setLocation(new Point(100, 6));
			txtRegistry.setText("ID");
			txtRegistry.setSize(new Dimension(110, 20));
		}
		return txtRegistry;
	}
	private JTextField getTxtInterval() {
		if (txtInterval == null) {
			txtInterval = new JTextField();
			txtInterval.setLocation(new Point(101, 29));
			txtInterval.setText("30");
			txtInterval.setSize(new Dimension(110, 20));
		}
		return txtInterval;
	}
	private JTextField getTxtSocket() {
		if (txtSocket == null) {
			txtSocket = new JTextField();
			txtSocket.setLocation(new Point(344, 6));
			txtSocket.setText("1234");
			txtSocket.setSize(new Dimension(95, 20));
		}
		return txtSocket;
	}

	private void ConnectClicked()
	{
		btnConnect.setEnabled(false);
		new RegistryServer(txtRegistry.getText(), txtSocket.getText(),
				txtInterval.getText(), this, txtConfigFile.getText()).start();
	}
	public void print(String txt)
	{
		if(jTextArea != null)
		{
			jTextArea.append(txt + "\n");
		}
	}

	private JTextField getTxtConfigFile() {
		if (txtConfigFile == null) {
			txtConfigFile = new JTextField();
			txtConfigFile.setSize(new Dimension(197, 20));
			txtConfigFile.setText("C:\\Temp\\config.xml");
			txtConfigFile.setLocation(new Point(345, 29));
		}
		return txtConfigFile;
	}
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
			jTextArea.setEditable(false);
		}
		return jTextArea;
	}
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJTextArea());
		}
		return jScrollPane;
	}
	public static void main(String[] args) {
		JFrame registry = rUI.getJFrame();
		registry.setVisible(true);
	}// main
}
